package mtm.test;

import java.awt.BasicStroke;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.MouseInputAdapter;

import mtm.ui.resizable.ResizableLabel;

/**
 * JFrame to display a week
 * @author CondorHero89
 *
 */
public class WeekFrame extends JFrame {
	
	public static int DAY_WIDTH = 150;
	public static int HOUR_HEIGHT = 24;
	
	final static BasicStroke dashed = new BasicStroke(
			1.0f, 
            BasicStroke.CAP_BUTT, 
            BasicStroke.JOIN_MITER, 
            10.0f, new float[] {1.0f}, 0.0f);
	
//	private ResizableLabel currentLabel;
	private static ArrayList<ResizableLabel> labelsList = new ArrayList<ResizableLabel>();

	// TODO for scroll function
//	private int scroll = 0;
	
	private JPanel panel = new JPanel(null) {
		
		private ResizableLabel intersectedLabel;	// the label is intesected with another label
		
		@Override
		public void paint(Graphics g) {
			super.paint(g);
			// TODO for scroll function
//			if (scroll > 0) {
//				g.translate(0, -12 * scroll);
//			}
			
			// draw grid
			Graphics2D g2 = (Graphics2D) g;
			// draw columns
			for (int i = 1; i <= 7; ++i) {
				g2.drawLine(i * DAY_WIDTH, 0, i * DAY_WIDTH, 24 * HOUR_HEIGHT);
			}
			// draw rows
			for (int i = 2; i <= 24; i += 2) {
				g2.drawLine(0, i * HOUR_HEIGHT, 7 * DAY_WIDTH, i * HOUR_HEIGHT);
			}
			g2.setStroke(dashed);
			for (int i = 1; i <= 24; i += 2) {
				g2.drawLine(0, i * HOUR_HEIGHT, 7 * DAY_WIDTH, i * HOUR_HEIGHT);
			}
			
			// draw labels
			for (ResizableLabel label : labelsList) {
				if (label.isIntersected()) {
					System.out.println("ok");
					intersectedLabel = label;
				} else {
					label.repaint();
				}
			}
			// draw the intersected label
			if (intersectedLabel != null)
				intersectedLabel.repaint();
		}
	};

	
	/**
	 * Constructor
	 */
	public WeekFrame() {
		add(panel);
		
		// set hand cursor
		setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

		// process mouse event
		addMouseListener(mouseListener);
		addMouseMotionListener(mouseListener);
		
		// other settings
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(new Dimension(DAY_WIDTH * 7 + 100, 24 * HOUR_HEIGHT + 100));
		setTitle("Tuần");
		setLocationRelativeTo(null);
		setVisible(true);
		
		// TODO for scroll function
		addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
//				if (e.getKeyCode() == KeyEvent.VK_UP) {
//					scroll++;
//					panel.repaint();
//				}
			}
		});
	}
	
	
	/**
	 * Return an array list of all label's location on the frame
	 * which is not moving
	 * @return
	 */
	public static ArrayList<Rectangle> getLabelsBound(Rectangle currentBound) {
		ArrayList<Rectangle> result = new ArrayList<Rectangle>();
		for (ResizableLabel lbl : labelsList) {
			if (!lbl.getBounds().equals(currentBound)) {
				result.add(lbl.getBounds());
			}
		}
		
		return result;
	}
	
	
	/**
	 * Listener for mouse event
	 */
	MouseInputAdapter mouseListener = new MouseInputAdapter() {
		private int x, y;
		private int h;
		
		@Override
		public void mousePressed(MouseEvent e) {
			x = e.getX();
			y = e.getY();
		}
		
		@Override
		public void mouseDragged(MouseEvent e) {}
		
		@Override
		public void mouseReleased(MouseEvent e) {
			h = e.getPoint().y - y;
			processMouseDrag(x, y, h);
		}
	};
	
	
	/**
	 * Process mouse drag event
	 * @param x
	 * @param y
	 * @param h
	 */
	private void processMouseDrag(int x, int y, int h) {
		if (h < 0) {
			int yOld = y;
			h = -h;
			y = round(y - h, HOUR_HEIGHT) - HOUR_HEIGHT;
			x = round(x, DAY_WIDTH);
			h = round( (yOld - h) % HOUR_HEIGHT + h, HOUR_HEIGHT ) + HOUR_HEIGHT;
		} else {
			int yOld = y;
			x = round(x, DAY_WIDTH);
			y = round(y, HOUR_HEIGHT) - HOUR_HEIGHT;
			h = round( (yOld - y) + h, HOUR_HEIGHT );
		}
			
		// create new label on frame
		ResizableLabel lbl = new ResizableLabel();
		lbl.setBounds(x, y, DAY_WIDTH, h);
		panel.add(lbl);
		panel.repaint();
		labelsList.add(lbl);
	}
	
	private int round(int x, int n) {
		return (x / n) * n;
	}

	public static void main(String[] args) {
		WeekFrame rc = new WeekFrame();
		rc.setVisible(true);
	}
}
